package comg.springboot.log.demo.controller;

import comg.springboot.log.demo.annotation.SysControllerLog;
import comg.springboot.log.demo.pojo.User;
import comg.springboot.log.demo.service.LogService;
import comg.springboot.log.demo.util.RandomPhoneNumber;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @Author: jxm
 * @Description:
 * @Date: 2022/4/15 16:21
 * @Version: 1.0
 */
@RestController
@Slf4j
public class LoginController {

    @Autowired
    LogService logService;


    @GetMapping({"/login","/"})
    public String login(@RequestParam(required = false) String userName,
                        @RequestParam(required = false) String password,
                        HttpServletRequest request){
        HttpSession session = request.getSession();
        if(StringUtils.hasLength(password) && StringUtils.hasLength(userName)){
            int number = (int)(Math.random() * 10);
            int number2 = (int)(Math.random() * 100);
            User user = new User();
            user.setId(number);
            user.setUserName(userName);
            user.setPhone(RandomPhoneNumber.createMobile(0));
            session.setAttribute("loginUser",user);
        }
        return "登录成功";
    }



    @SysControllerLog(desc = "主页")
    @GetMapping("/main")
    public String main(@RequestParam(required = false) String param1,
                       @RequestParam(required = false) String param2){

        log.info("----------main --------------参数1{},参数2{}",param1,param2);
        int i = 10/0;

        return "main";
    }






    @GetMapping("/logout")
    public String logout(){
        return "logout";
    }
}
